package com.iknow.view.widget.zoom;

import android.os.Handler;
import android.os.SystemClock;
import com.iknow.util.SpringDynamics;
import java.util.Observable;
import java.util.Observer;

public class DynamicZoomControl
  implements Observer
{
  private static final int FPS = 50;
  private static final float MAX_ZOOM = 16.0F;
  private static final float MIN_ZOOM = 1.0F;
  private static final float PAN_OUTSIDE_SNAP_FACTOR = 0.40000000596046448F;
  private static final float REST_POSITION_TOLERANCE = 0.0099999997764825821F;
  private static final float REST_VELOCITY_TOLERANCE = 0.0040000001899898052F;
  private AspectQuotient mAspectQuotient;
  private final Handler mHandler;
  private final SpringDynamics mPanDynamicsX;
  private final SpringDynamics mPanDynamicsY;
  private float mPanMaxX;
  private float mPanMaxY;
  private float mPanMinX;
  private float mPanMinY;
  private final ZoomState mState = new ZoomState();
  private final Runnable mUpdateRunnable;

  public DynamicZoomControl()
  {
    this.mPanDynamicsX = new SpringDynamics();
    this.mPanDynamicsY = new SpringDynamics();
    this.mHandler = new Handler();
    this.mUpdateRunnable = new Runnable(this)
    {
      public void run()
      {
        int i;
        long l1 = SystemClock.uptimeMillis();
        DynamicZoomControl.access$0(this.this$0).update(l1);
        DynamicZoomControl.access$1(this.this$0).update(l1);
        if ((DynamicZoomControl.access$0(this.this$0).isAtRest(0.0040000001899898052F, 0.0099999997764825821F)) && (DynamicZoomControl.access$1(this.this$0).isAtRest(0.0040000001899898052F, 0.0099999997764825821F)))
          i = 1;
        while (true)
        {
          DynamicZoomControl.access$2(this.this$0).setPanX(DynamicZoomControl.access$0(this.this$0).getPosition());
          DynamicZoomControl.access$2(this.this$0).setPanY(DynamicZoomControl.access$1(this.this$0).getPosition());
          if (i == 0)
          {
            long l2 = SystemClock.uptimeMillis();
            DynamicZoomControl.access$3(this.this$0).postDelayed(DynamicZoomControl.access$4(this.this$0), 20L - l2 - l1);
          }
          DynamicZoomControl.access$2(this.this$0).notifyObservers();
          return;
          i = 0;
        }
      }
    };
    this.mPanDynamicsX.setFriction(2F);
    this.mPanDynamicsY.setFriction(2F);
    this.mPanDynamicsX.setSpring(50.0F, 1F);
    this.mPanDynamicsY.setSpring(50.0F, 1F);
  }

  private float getMaxPanDelta(float paramFloat)
  {
    return Math.max(0F, 0.5F * (paramFloat - 1F) / paramFloat);
  }

  private void limitZoom()
  {
    if (this.mState.getZoom() < 1F)
      this.mState.setZoom(1F);
    while (true)
    {
      do
        return;
      while (this.mState.getZoom() <= 16.0F);
      this.mState.setZoom(16.0F);
    }
  }

  private void updatePanLimits()
  {
    float f1 = this.mAspectQuotient.get();
    float f2 = this.mState.getZoomX(f1);
    float f3 = this.mState.getZoomY(f1);
    this.mPanMinX = (0.5F - getMaxPanDelta(f2));
    this.mPanMaxX = (0.5F + getMaxPanDelta(f2));
    this.mPanMinY = (0.5F - getMaxPanDelta(f3));
    this.mPanMaxY = (0.5F + getMaxPanDelta(f3));
  }

  public ZoomState getZoomState()
  {
    return this.mState;
  }

  public void pan(float paramFloat1, float paramFloat2)
  {
    float f1 = this.mAspectQuotient.get();
    float f2 = paramFloat1 / this.mState.getZoomX(f1);
    float f3 = paramFloat2 / this.mState.getZoomY(f1);
    if (((this.mState.getPanX() > this.mPanMaxX) && (f2 > 0F)) || ((this.mState.getPanX() < this.mPanMinX) && (f2 < 0F)))
      f2 *= 0.40000000596046448F;
    if (((this.mState.getPanY() > this.mPanMaxY) && (f3 > 0F)) || ((this.mState.getPanY() < this.mPanMinY) && (f3 < 0F)))
      f3 *= 0.40000000596046448F;
    float f4 = f2 + this.mState.getPanX();
    float f5 = f3 + this.mState.getPanY();
    this.mState.setPanX(f4);
    this.mState.setPanY(f5);
    this.mState.notifyObservers();
  }

  public void setAspectQuotient(AspectQuotient paramAspectQuotient)
  {
    if (this.mAspectQuotient != null)
      this.mAspectQuotient.deleteObserver(this);
    this.mAspectQuotient = paramAspectQuotient;
    this.mAspectQuotient.addObserver(this);
  }

  public void startFling(float paramFloat1, float paramFloat2)
  {
    float f = this.mAspectQuotient.get();
    long l = SystemClock.uptimeMillis();
    this.mPanDynamicsX.setState(this.mState.getPanX(), paramFloat1 / this.mState.getZoomX(f), l);
    this.mPanDynamicsY.setState(this.mState.getPanY(), paramFloat2 / this.mState.getZoomY(f), l);
    this.mPanDynamicsX.setMinPosition(this.mPanMinX);
    this.mPanDynamicsX.setMaxPosition(this.mPanMaxX);
    this.mPanDynamicsY.setMinPosition(this.mPanMinY);
    this.mPanDynamicsY.setMaxPosition(this.mPanMaxY);
    this.mHandler.post(this.mUpdateRunnable);
  }

  public void stopFling()
  {
    this.mHandler.removeCallbacks(this.mUpdateRunnable);
  }

  public void update(Observable paramObservable, Object paramObject)
  {
    limitZoom();
    updatePanLimits();
  }

  public void zoom(float paramFloat1, float paramFloat2, float paramFloat3)
  {
    float f1 = this.mAspectQuotient.get();
    float f2 = this.mState.getZoomX(f1);
    float f3 = this.mState.getZoomY(f1);
    this.mState.setZoom(paramFloat1 * this.mState.getZoom());
    limitZoom();
    float f4 = this.mState.getZoomX(f1);
    float f5 = this.mState.getZoomY(f1);
    this.mState.setPanX(this.mState.getPanX() + (paramFloat2 - 0.5F) * (1F / f2 - 1F / f4));
    this.mState.setPanY(this.mState.getPanY() + (paramFloat3 - 0.5F) * (1F / f3 - 1F / f5));
    updatePanLimits();
    this.mState.notifyObservers();
  }
}